﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MentorBackEnd.Model;
using System.Data.SqlClient;
using System.Data;

namespace MentorBackEnd.DA
{
    public class UserProfileDA
    {
        public static void CreateNewUserProfile(UserProfile userProfile)
        {
            SqlConnection conn = DataAccessor.GetConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_UserProfileInsert]");
                cmd.Parameters.Add(new SqlParameter("@UserProfileID", userProfile.UserProfileID));
                cmd.Parameters.Add(new SqlParameter("@MeMaUserID", userProfile.MeMaUserID)); 
                cmd.Parameters.Add(new SqlParameter("@MentorStatusTypeID", (int) userProfile.MentorStatusType));
                cmd.Parameters.Add(new SqlParameter("@MentorRating", userProfile.MentorRating));
                cmd.Parameters.Add(new SqlParameter("@LinkedInLink", userProfile.LinkedInLink));
                cmd.Parameters.Add(new SqlParameter("@FacebookLink", userProfile.FacebookLink));
                cmd.Parameters.Add(new SqlParameter("@TwitterHandle", userProfile.TwitterHandle));
                cmd.Parameters.Add(new SqlParameter("@PersonalWebsite", userProfile.PersonalWebsite));
                cmd.Parameters.Add(new SqlParameter("@Summary", userProfile.Summary));

                cmd.ExecuteNonQuery();
            }

            catch { throw; }
            finally { conn.Close(); }
            return;
        }


        public static void UpdateUserProfile(UserProfile userProfile)
        {
            SqlConnection conn = DataAccessor.GetConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_UserProfileUpdate]");
                cmd.Parameters.Add(new SqlParameter("@UserProfileID", userProfile.UserProfileID));
                cmd.Parameters.Add(new SqlParameter("@MeMaUserID", userProfile.MeMaUserID)); 
                cmd.Parameters.Add(new SqlParameter("@MentorStatusTypeID", (int)userProfile.MentorStatusType));
                cmd.Parameters.Add(new SqlParameter("@MentorRating", userProfile.MentorRating));
                cmd.Parameters.Add(new SqlParameter("@LinkedInLink", userProfile.LinkedInLink));
                cmd.Parameters.Add(new SqlParameter("@FacebookLink", userProfile.FacebookLink));
                cmd.Parameters.Add(new SqlParameter("@TwitterHandle", userProfile.TwitterHandle));
                cmd.Parameters.Add(new SqlParameter("@PersonalWebsite", userProfile.PersonalWebsite));
                cmd.Parameters.Add(new SqlParameter("@Summary", userProfile.Summary));

                cmd.ExecuteNonQuery();
            }

            catch { throw; }
            finally { conn.Close(); }
            return;
        }

        public static UserProfile GetUserProfile(int userID)
        {
            SqlConnection conn = DataAccessor.GetConnection();
            IDataReader reader = null;
            List<UserProfile> collection = new List<UserProfile>();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_UserProfileSelect]");
                cmd.Parameters.Add(new SqlParameter("@UserID",userID));
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    UserProfile dataModel = new UserProfile(reader);
                    collection.Add(dataModel);

                }

                if (!reader.IsClosed)
                    reader.Close();

                if (collection.Count > 0)
                    return collection[0];
                else
                    return null;
            }

            catch { throw; }
            finally { conn.Close(); }
        }


    }
}